<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<!-- head 头部-->
<head th:replace="admin/layout/include::head('栏目管理','')"></head>

<body>
<!-- loading 动画-->
<div th:replace="admin/layout/include::loading"></div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="pull-left layui-form-pane search-box">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="btnExpandAll" class="layui-btn layui-btn-primary">
                            <i class="layui-icon layui-icon-shrink-right"></i>展开全部
                        </button>
                        <button id="btnFoldAll" class="layui-btn layui-btn-primary">
                            <i class="layui-icon layui-icon-spread-left"></i>折叠全部
                        </button>
                    </div>
                </div>
            </div>


            <!-- 数据表格 -->
            <table class="layui-table" id="data-table" lay-filter="data-table"></table>
        </div>
    </div>
</div>
</body>

<!-- 表格操作列 -->
<script type="text/html" id="data-table-bar">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="type">分类管理</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="data-table-state">
    <input type="checkbox" lay-filter="data-table-state" value="{{d.catalogId}}" lay-skin="switch" lay-text="正常|禁用"
           {{d.state==0?'checked':''}}/>
</script>
<!--类型-->
<script type="text/html" id="data-table-type">
    {{#  if(d.catalogType == "1"){ }}
    <span style="color: #009688;">网站菜单</span>
    {{#  } else if(d.catalogType == "2"){ }}
    <span style="color: #5FB878;">top导航</span>
    {{#  } else if(d.catalogType == "3"){ }}
    <span style="color: #1E9FFF;">底部导航</span>
    {{#  } else{ }}
    unkown
    {{#  }}}
</script>
<script type="text/html" id="data-table-indexShow">
    {{#  if(d.indexShow == "1"){ }}
    展示
    {{#  } else{ }}
    隐藏
    {{#  }}}
</script>
<script type="text/html" id="data-table-target">
    {{#  if(d.target == "_self"){ }}
    当前窗口打开
    {{#  } else if(d.target == "_blank"){ }}
    新窗口打开
    {{#  } else{ }}
    unkown
    {{#  }}}
</script>
<!-- js部分 -->
<div th:replace="admin/layout/include::js"></div>
<script>
    layui.use(['form', 'table', 'util', 'admin','treeTable'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var treeTable = layui.treeTable;
        var formUrl;  // 表单提交的action
        var insTb = treeTable.render({
            elem: '#data-table',
            tree: {
                isPidData:true,
                iconIndex: 2, // 是否是pid形式数据
                idName: 'catalogId',
                pidName: 'parentId'
            },
            height : 'full-165',
            reqData: function(data, callback) {
                // 在这里写ajax请求，通过callback方法回调数据
                $.get('/admin/catalog/tree', function (res) {
                    callback(res.data);  // 参数是数组类型
                });
            },
            cols: [
                {type: 'checkbox'},
                {field: 'catalogId', width:50,title: '#'},
                {field: 'catalogName', minWidth: 150, title: '栏目名称'},
                {field: 'indexShow',  title: '首页展示',templet:'#data-table-indexShow'},
                {field: 'catalogDesc',width:300, title: '描述',style:'overflow:hidden;white-space:nowrap;'},
                {field: 'state',title:'状态',templet:'#data-table-state'},
                {field: 'orderNo', width: 80, align: 'center', title: '排序'},
                {templet: '#data-table-bar', width: 200, align: 'center', title: '操作'}
            ],
            text: {
                none: '<div style="padding: 15px 0;">哎呀，一条数据都没有~</div>'
            }
        });
        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            showEditModel();
        });
        $('#btnExpandAll').click(function () {
            insTb.expandAll();  // 展开全部
        })
        $('#btnFoldAll').click(function () {
            insTb.foldAll();  // 折叠全部
        })

        // 工具条点击事件
        treeTable.on('tool(data-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { //删除
                doDelete(obj);
            }else if(obj.event === 'type'){
                //类型管理
                showTypeModel(data.catalogId);
            }
        });

        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var keyword = $('#edtSearch').val();
            insTb.filterData(keyword);   // 根据关键字模糊搜索
        });

        function doDelete(obj){
            top.layer.confirm('确定要删除吗？', function (i) {
                top.layer.close(i);
                B.del({
                    url:'/admin/catalog/'+obj.data.catalogId,
                    loading:true,
                    success:function(data){
                        if (data.success) {
                            layer.msg(data.msg, {icon: 1});
                            obj.del();
                            insTb.refresh()
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }
                });
            });
        }
        // 显示编辑弹窗
        var showEditModel = function (data) {
            admin.putTempData("t_catalog",data);
            admin.putTempData("formOk",false);

             var index = layer.open({
               title : data ? '修改栏目' : '添加栏目',
                type : 2,
                content : '/admin/catalog/save',
                area : [ '100%', '100%' ],
                success : function(layero, index){
                    //var body = layui.layer.getChildFrame('body', index);

                    setTimeout(function(){
                        layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500)
                },
                end: function () {
                     admin.getTempData('formOk') && insTb.refresh();  // 成功刷新表格
                 }
            });
            layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layer.full(index);
            })

        };
        var showTypeModel=function(catalogId){
            admin.putTempData("catalogId",catalogId);
            var index = layer.open({
                title : '栏目分类管理',
                type : 2,
                content : '/admin/catalog/type',
                area : [ '100%', '100%' ],
                success : function(layero, index){
                    //var body = layui.layer.getChildFrame('body', index);

                    setTimeout(function(){
                        layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500)
                }
            })
            layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layer.full(index);
            })
        }
        // 修改状态
        form.on('switch(data-table-state)', function (obj) {
            B.post({
                url: '/admin/catalog/updateState',
                loading: true,
                data: {catalogId: obj.elem.value, state: obj.elem.checked ? 0 : 1},
                success: function (data) {
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                }

            });

        });

    });

</script>
</html>